package com.pi.community.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.pi.base.enumerate.record.RecordState;
import com.pi.base.exception.ServiceException;
import com.pi.comm.sequence.SnowflakeFactory;
import com.pi.community.dao.entity.BaseCommunityEntity;
import com.pi.community.dao.mapper.BaseCommunityMapper;
import com.pi.community.dao.param.BaseCommunityParam;
import com.pi.community.vo.CommunityVo;
import com.pi.config.dao.entity.BaseNameIdxEntity;
import com.pi.config.service.BaseNameIdxService;

@Service
public class CommunityService {
  private BaseCommunityMapper baseCommunityMapper;
  private BaseNameIdxService baseNameIdxService;
  public CommunityService(BaseCommunityMapper baseCommunityMapper, BaseNameIdxService baseNameIdxService){
    Assert.notNull(baseCommunityMapper, "社区基础服务为空");
    Assert.notNull(baseNameIdxService, "名称索引基础服务为空");
    this.baseCommunityMapper = baseCommunityMapper;
    this.baseNameIdxService = baseNameIdxService;
  }
  
  public List<CommunityVo> searchCommunity(String name){
    if(StringUtils.isBlank(name)){
      return new ArrayList<>(0);
    }
    List<BaseNameIdxEntity> nameIndexList = baseNameIdxService.searchNameEntity(1, name);
    if(CollectionUtils.isEmpty(nameIndexList)){
        return Collections.emptyList();
    }
    List<Long> conmunityIds = nameIndexList.stream().map(BaseNameIdxEntity::getNameId).collect(Collectors.toList());
    List<BaseCommunityEntity> conmunityList = baseCommunityMapper.getByIds(conmunityIds);
    if(CollectionUtils.isEmpty(conmunityList)){
        return Collections.emptyList();
    }
    List<CommunityVo> voList = new ArrayList<>(conmunityList.size());
    for(BaseCommunityEntity entity : conmunityList){
        CommunityVo communityVo = new CommunityVo();
        communityVo.setId(entity.getId());
        communityVo.setName(entity.getCommunityName());
        voList.add(communityVo);
    }
    return voList;
  }

    public Long addCommunity(
            @NotBlank(message="社区名称不能为空")
            @Length(max = 30) String name,
            @NotBlank(message="社区地址不能为空")
            @Length(max = 250) String address) {
        BaseCommunityParam param = new BaseCommunityParam();
        param.setCommunityName(name);
        //param.setState(RecordState.STATE_NORMAL.getState());
        List<BaseCommunityEntity> list = baseCommunityMapper.findList(param);
        if(!CollectionUtils.isEmpty(list) && list.get(0).getState()){
            throw new ServiceException("社区已存在");
        }
        BaseCommunityEntity entity = new BaseCommunityEntity();
        entity.setCommunityName(name);
        entity.setCommunityAddress(address);
        entity.setState(RecordState.STATE_NORMAL.getState());
        if(!CollectionUtils.isEmpty(list)){
            entity.setId(list.get(0).getId());
            entity.setVersion(list.get(0).getVersion() + 1);
            baseCommunityMapper.updateById(entity);
        }else{
            entity.setId(SnowflakeFactory.getDefaultWorker().nextId());
            baseCommunityMapper.insert(entity);
        }
        return entity.getId();
    }
    
    public BaseCommunityEntity findOne(@NotNull(message = "社区编号不能为空") Long id){
        BaseCommunityEntity entity = baseCommunityMapper.findOne(id);
        return null == entity || !entity.getState() ? null : entity;
    }

    /**
     * 根据联系方式查询物业
     * @param contact
     * @return
     */
    public BaseCommunityEntity findByContact(@NotNull(message = "联系方式不能为空")String contact){
        BaseCommunityParam param = new BaseCommunityParam();
        param.setContact(contact);
        param.setState(RecordState.STATE_NORMAL.getState());
        List<BaseCommunityEntity> list = baseCommunityMapper.findList(param);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    /**
     * 更新BaseCommunityEntity
     * @param baseCommunityEntity
     */
    public int updateById(BaseCommunityEntity baseCommunityEntity ){
        return baseCommunityMapper.updateById(baseCommunityEntity);
    }
}
